﻿using BootyBay.Models;
using BootyBay.Repository;
using System.Collections.Generic;
using System.Linq;

namespace BootyBay.ServerWebApp.WCFSvcs
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "StoreServices" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select StoreServices.svc or StoreServices.svc.cs at the Solution Explorer and start debugging.
    public class StoreServices : IStoreServices
    {
        //private List<ApplicationVersionInfo> GenerateTestAppInfoData()
        //{
        //    List<ApplicationVersionInfo> appList = new List<ApplicationVersionInfo>();

        //    ApplicationVersionInfo appOne = new ApplicationVersionInfo()
        //    {
        //        DisplayName = "Top App Showcase Demo App",
        //        //CategoryName = "Test",
        //        Description = "This is for test. App package is stored on Windows Azure Storage",
        //        IsInPublicWindowsStore = false,
        //        //KeyWords = { "", "", "" },
        //        PackageLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/TopAppShowcaseDemo_1.0.0.0_AnyCPU_Debug.appx",
        //        //ScreenshotUrls = { "", "", "" },
        //        Version = new Version(1, 0, 0, 0).ToString(),
        //        IdentityName = "bbe1e991-d938-4482-a80c-01ea12de35d3",
        //        PackageCertLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/TopAppShowcaseDemo_1.0.0.0_AnyCPU_Debug.cer"
        //    };

        //    ApplicationVersionInfo appOneNewVersion = new ApplicationVersionInfo()
        //    {
        //        DisplayName = "Top App Showcase Demo App",
        //        //CategoryName = "Test",
        //        Description = "This is for upgrading test.  App package is stored on Windows Azure Storage. Nothing changed but the verison number",
        //        IsInPublicWindowsStore = false,
        //        //KeyWords = { "", "", "" },
        //        PackageLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/TopAppShowcaseDemo_1.0.0.1_AnyCPU_Debug.appx",
        //        //ScreenshotUrls = { "", "", "" },
        //        Version = new Version(1, 0, 0, 1).ToString(),
        //        IdentityName = "bbe1e991-d938-4482-a80c-01ea12de35d3",
        //        PackageCertLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/TopAppShowcaseDemo_1.0.0.1_AnyCPU_Debug.cer"
        //    };

        //    ApplicationVersionInfo appTwo = new ApplicationVersionInfo()
        //    {
        //        DisplayName = "BootyBay Portal App",
        //        //CategoryName = "Test",
        //        Description = "BootyBay Portal App Description",
        //        IsInPublicWindowsStore = false,
        //        //KeyWords = { "", "", "" },
        //        PackageLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/BootyBay.PortalApp_1.0.0.0_AnyCPU_Debug.appx",
        //        //ScreenshotUrls = { "", "", "" },
        //        Version = new Version(1, 0, 0, 0).ToString(),
        //        IdentityName = "4eee5a62-26ee-4c1f-ab12-64ef8a765cfa"
        //    };

        //    //

        //    ApplicationVersionInfo appThree = new ApplicationVersionInfo()
        //    {
        //        DisplayName = "HealthCare App",
        //        //CategoryName = "LOB app",
        //        Description = "HealthCare App Description",
        //        IsInPublicWindowsStore = false,
        //        //KeyWords = { "", "", "" },
        //        PackageLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/HealthCare_1.0.0.15_AnyCPU.appx",
        //        //ScreenshotUrls = { "", "", "" },
        //        Version = new Version(1, 0, 0, 15).ToString(),
        //        IdentityName = "MSITLoguKrishnan.AditiHealthcareShowcase",
        //        PackageCertLocationUrl = "https://bootybay.blob.core.windows.net/bootybaystore/HealthCare_1.0.0.15_AnyCPU.cer"
        //    };


        //    appList.Add(appOne);
        //    appList.Add(appOneNewVersion);
        //    appList.Add(appTwo);
        //    appList.Add(appThree);

        //    return appList;
        //}

        public List<View_Application> GetAllAppInfoData()
        {
            var appversionInfoList = ApplicationRepository.Instance().GetAllValidApplications();

            var data = appversionInfoList.Select(a => a as View_Application).ToList();
            return data;//.Select(a => new ApplicationVersionInfoSvcModel { Name = a.DisplayName }).ToList();

        }

        public ApplicationVersionInfo GetVersionInfo(int appVersionInfoId)
        {
            var appVerInfo = ApplicationInfoRepository.Instance().GetVersionInfoByAppVersionId(appVersionInfoId);
            return appVerInfo;
        }

        public ApplicationProcessorArchitecture GetPackageUrl(int appVersionInfoId, int processArchitecture)
        {
            var package = AppArchitectureRepository.Instance().GetPackageByArchitecture(appVersionInfoId, processArchitecture);
            return package;
        }

        public List<Category> GetAllCategories()
        {
            RepositoryBase<Category> repository = new RepositoryBase<Category>();
            var list = repository.GetAll().ToList();
            return list;
        }
        public List<View_Application> GetRecommendedApps()
        {
            var list = ApplicationRepository.Instance().GetAllValidApplications()
                .Where(a => a.IsRecommended).ToList();
            return list;
        }

        public List<View_Application> GetPromotionalApps()
        {
            var list = ApplicationRepository.Instance().GetAllValidApplications().Take(5).ToList();
            return list;
        }

        /// <summary>
        /// Ranger: always set IsValid = True for demo project.
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public View_StoreUser ValidateUser(string userName)
        {
            //RepositoryBase<StoreUser> userRepo = new RepositoryBase<StoreUser>();

            //var storeUser = userRepo.GetAll().Where(u => u.UserName.Equals(userName)).FirstOrDefault();

            View_StoreUser outputUser = new View_StoreUser() { UserName = userName,  IsValid=true };
            //if (storeUser == null)
            //    outputUser.IsValid = false;
            //else
            //    outputUser.IsValid = storeUser.IsValid;

            return outputUser;

        }
        //public List<ApplicationVersionInfo> GetUserPurchasedAppInfoData(string userName, string pwd)
        //{
        //    return null;
        //}

        //public List<ApplicationVersionInfo> GetAppInfoDataRequestingInstall(string userName, string pwd)
        //{
        //    return null;
        //}

        //public List<StoreAppMgmtTask> GetAppMgmtTasksInQueueForUser(string userName, string pwd)
        //{
        //    var tasks = StoreAppMgmtTaskRepository.Instance().GetAllAppMgmtTasks(userName, AppOperationStatus.RequestSubmitted);

        //    return tasks;

        //}

        //public void ReportTaskStatus(Guid taskID, AppOperationStatus status, string message)
        //{

        //    StoreAppMgmtTaskRepository.Instance().UpdateTaskStatus(taskID, status);

        //    Debug.WriteLine("ReportTaskStatus: Task ID- " + taskID + ", status-" + status.ToString() + ", message-" + message);
        //}


        //public Guid SubmitClientAppOperationRequest(int appVerInfoID, AppOperationType optType, string userName, string pwd)
        //{
        //    return StoreAppMgmtTaskRepository.Instance().SubmitAppMgmtTask(appVerInfoID, optType, userName);
        //}

        //public StoreAppMgmtTask GetTaskStatus(Guid taskID)
        //{
        //    return StoreAppMgmtTaskRepository.Instance().GetTaskStatus(taskID);
        //}




    }

    //public class ApplicationVersionInfoSvcModel
    //{
    //    public string Name { get; set; }
    //}

}
